//
//  CoolMathAppDelegate.h
//  CoolMath
//
//  Created by e-linkway on 10-4-30.
//  Copyright Apple 2010. All rights reserved.
//

#import "SoundWrapper.h"


@implementation SoundWrapper

+ (id)soundEffectWithContentsOfFile: (NSString *)aPath 
{
    if (aPath) 
    {
        return [[[SoundWrapper alloc] initWithContentsOfFile:aPath] autorelease];
    }

    return nil;
}

- (id)initWithContentsOfFile: (NSString *)path 
{
    self = [super init];
    
    if (self != nil) {
        NSURL *aFileURL = [NSURL fileURLWithPath:path isDirectory:NO];
        
        if (aFileURL != nil)  {
            SystemSoundID aSoundID;
            OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
            
            if (error == kAudioServicesNoError) { // success
                _soundID = aSoundID;
            } else {
                NSLog(@"Error %ld loading sound at path: %@", error, path);
                [self release], self = nil;
            }
        } else {
            NSLog(@"NSURL is nil for path: %@", path);
            [self release], self = nil;
        }
    }
    return self;
}

-(void)dealloc {
    AudioServicesDisposeSystemSoundID(_soundID);
    [super dealloc];
}

-(void) play {
    AudioServicesPlaySystemSound(_soundID);
}


@end
